import { Controller, Get, Post, Injectable, Body, Put, Delete, Param } from '@nestjs/common';
import { InjectModel } from 'nestjs-typegoose';
import {Post as PostSchema} from './post.model'
import { ReturnModelType} from '@typegoose/typegoose';
import { ApiTags, ApiOperation, ApiProperty } from '@nestjs/swagger';
import { IsNotEmpty } from 'class-validator'

export class CreatePostDto {
    @ApiProperty()
    @IsNotEmpty({message:'请填写标题'})
    title:string

    @ApiProperty()
    content:string

}

@Injectable()
@Controller('posts')
@ApiTags('文章')
export class PostsController {

    constructor(
        @InjectModel(PostSchema) private readonly PostModel: ReturnModelType<typeof PostSchema>
      ) {}

@Get()
@ApiOperation({summary:'获取文章详情'})
async index(){
    return  await this.PostModel.find()
}

@Post()
@ApiOperation({summary:'创建文章'})
async created(@Body() createdPostDto:CreatePostDto) {
    await this.PostModel.create(createdPostDto)
    return {
        success:true
    }
}

@Get(':id')
@ApiOperation({summary:'获取文章详情'})
async dateil(@Param('id') id:string){
    return await this.PostModel.findById(id)
}

@Put(':id')
@ApiOperation({summary:'更新文章'})
async update(@Param('id') id:string ,@Body() createPostDto:CreatePostDto){
    await this.PostModel.findByIdAndUpdate(id,createPostDto)
    return{
        success:true
    }
}

@Delete(':id')
@ApiOperation({summary:'删除文章'})
async remove(@Param('id') id:string){
    await this.PostModel.findByIdAndDelete(id)
    return {
        success:true
    }
}


}
